package com.ftg.learn.service.impl;

import com.ftg.learn.domain.DemoDomain;
import com.ftg.learn.mapper.DemoMapper;
import com.ftg.learn.service.DemoService;
import com.ftg.learn.util.PageEx;
import com.ftg.learn.util.ToWeb;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author kangjx
 * @date 2024/1/30
 */
@Service
public class DemoServiceImpl implements DemoService {

    @Resource
    @Qualifier("demoMapper")
    DemoMapper demoMapper;

    public ToWeb<PageEx<DemoDomain>> showDemo(String name, long pageNo, long pageSize) {
        PageEx<DemoDomain> data = new PageEx<>(pageNo,pageSize);

        data.setTotal(demoMapper.demoCount(name));

        data.setRows(demoMapper.showDemo(name, (pageNo - 1) * pageSize, pageSize));
        //分页 total  List<Domain>

        return ToWeb.success(data);
    }

    @Override
    public int insertDemo(DemoDomain domain) {
        return demoMapper.insertDemo(domain);
    }

    @Override
    public ToWeb<Integer> deleteById(Integer id) {

        if(Objects.nonNull(demoMapper.findOneById(id))){

            Integer num = demoMapper.deleteById(id);
            return num>0?ToWeb.success(num):ToWeb.failure(num);
        }
        return ToWeb.failure("对不起当前数据已经被删除了",0);
    }

    @Override
    public ToWeb<DemoDomain> findOneById(Integer id) {
        return ToWeb.success(demoMapper.findOneById(id));
    }

    @Override
    public ToWeb<Integer> update(DemoDomain domain) {
        if(Objects.nonNull(demoMapper.findOneById(domain.getId()))){
            return ToWeb.success(demoMapper.update(domain));
        }
        return ToWeb.failure("对不起当前数据已经被删除了",0);
    }
}
